package controllers;


import models.Profile;
import play.Play;
import play.libs.Json;
import play.mvc.*;
import wrapper.DropboxWrapper;


import static play.libs.Json.toJson;

public class FileYOUController extends Controller {
  	
  public static Result getAbout() {
	  String version = "Version = 2013.06.02: Searching ok! Acrescentei a opção de fazer download do arquivo. Tá bonito. Só falta navegar nas pastas";
	  version += "On host: " + Play.application().configuration().getString("filyou.host");
	  return ok (toJson(version));		  
  }
	  

  public static Result getUsers() {
	  try {
		  return ok (toJson(Profile.all()));
	  } catch (Exception e){
		  return badRequest("Error getting user' list.");
	  }
  }
  
  public static Result createUser() {
	  try {
		  Profile p = new Profile();
		  p.save();
		  
		  return ok (Json.parse("{\"id\": " + p.id +"}"));
		  
	  } catch (Exception e){
		  e.printStackTrace();
		  return badRequest("Error creating user." + e.getMessage());
	  }
	  
  }

  
  public static Result createUserBak(Long id) {
	  try {
		  Profile p = new Profile(id);
		  p.save();
		  
		  return ok (toJson("User created: " + id));
	  } catch (Exception e){
		  return badRequest("Error creating user " + id + ". " + e.getMessage());
	  }
  }
  
  public static Result getMetadata(Long id) {
	  //return TODO;
	  
	  try {
		  Profile p = Profile.find(id);
		  return ok (toJson(DropboxWrapper.getMetadata(p.dpkey, p.dpSecret, null)));
	  } catch (Exception e){
		  return badRequest("Error gettind data for user " + id + ". Are user account already created and linked?" +
		  		"\n\r" + e.getMessage());
	  }
  }
  
  public static Result getUniqueMetadata(Long id, String file) {
	  //return TODO;
	  
	  try {
		  
		  Profile p = Profile.find(id);
		  file = "/"+ file;
		  return ok (toJson(DropboxWrapper.getMetadata(p.dpkey, p.dpSecret, file)));
	  } catch (Exception e){
		  return badRequest("Error gettind data for user " + id + ". Are user account already created and linked?" +
			  		"\n\r" + e.getMessage());
	  }
  }
  
  public static Result searchFiles(Long id, String query) {
	  //return TODO;
	  try {
		  Profile p = Profile.find(id);
		  return ok (toJson(DropboxWrapper.search(p.dpkey, p.dpSecret, query, null)));
	  } catch (Exception e){
		  return badRequest("Error gettind data for user " + id + ". Are user account already created and linked?" +
			  		"\n\r" + e.getMessage());
	  }
  }
  
  /**
   * Use: host/filyou/{id}/download?path={path}
   * @param id
   * @param path
   * @return
   */
  public static Result downloadFile(Long id, String path) {
	  //return TODO;
	  try {
		  Profile p = Profile.find(id);
		  return ok (DropboxWrapper.getFile(p.dpkey, p.dpSecret, path));
	  } catch (Exception e){
		  return badRequest("Error downloading file: " + path + 
			  		"\n\r" + e.getMessage());
	  }
  }
  
  public static Result link(Long id){
	  return DropboxWrapper.link(id);
  }
  
  public static Result linkReturn(Long id, String uid, String oauth_token){
	  return DropboxWrapper.callBackAuth(id, uid, oauth_token);
  }
  
  public static Result unLink(Long id){
	  try {
		  
		  Profile p = Profile.find(id);
		  p.dpkey = null;
		  p.dpSecret = null;
		  p.save();
	  } catch (Exception e){
		  return badRequest("Error unlinking user account " + id + ". Are user account already created and linked?" +
		  		"\n\r" + e.getMessage());
	  }
	  
	  return ok("We deleted all information about your dropbox account stored in our database" +
	  		"\n\rBut to actually unlink dropbox account, you HAVE TO go to dropbox page and do it mannualy.");
	  		
  }
  
}